/*
*
* @file string.c
*
* Reindeer OS C Standart Library Implementation
* Standard C String routines implementation
*/

#include <string.h>

/** 
* strlen() - return length of string
* @param *char str Pointer to null terminated string of chars
*/
size_t strlen (const char * str) {
	size_t len=0;
	while (str[len++]);
	return len;
}

/**
* strcpy - copies string s2 to s1
* @param *char s1 Destination string
* @param const *char s2 Source string
*/
char * strcpy(char *s1, const char *s2) {
	char *s1_p = s1;
	while(*s1++ = *s2++);
	return s1_p;
}

/**
 * strcmp - returns 0 when the strings are equal, a negative
 * integer when s1 is less than s2, or a positive integer
 * if s1 is greater than s2, according to the lexicographical order.
 * @param s1 - string 1
 * @param s2 - string 2
 * @return int result of comparation
 */
int strcmp (const char * s1, const char * s2)
{
    for(; *s1 == *s2; ++s1, ++s2)
        if(*s1 == 0)
            return 0;
    return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}

/**
* memcpy() - copise count bytes from src to dest
* @param *void dest Destination copies to
* @param const *void Source pointer
* @param size_t count Size copies to
*/
void *memcpy(void *dest, const void *src, size_t count) {
	const char *sp = (const char *)src;
	char *dp = (char *)dest;
	for(; count !=0; count--) *dp++ = *sp++;
	return dest;
}

void *memset(void *dest, char val, size_t count) {
	unsigned char *temp = (unsigned char *)dest;
	for(; count != 0; count--) {
		*temp++ = val;
	}
	return dest;
}

/**
* memsetw() - sets count dest bytes to src
* @param unsigned *short dest Destination pointer
* @param unsigned short val Source value
* @param size_ count Count bytes
*/
unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count) {
	unsigned short *temp = (unsigned short *)dest;
	for(; count != 0; count--)
		*temp++ = val;
	return dest;
}
